function moba.boxcollision(x1,y1,w1,h1,xm,ym,x2,y2,w2,h2)
	local dx,dy
	if not(xm+w1 < x2 or ym+h1 < y2 or xm > x2+w2 or ym > y2+h2) then
		if x1 > x2+w2 or x1+w1 < x2 then
			dy = ym
		else
			dy = y1
		end
		if y1 > y2+h2 or y1+h1 < y2 then
			dx = xm
		else
			dx = x1
		end
	end
	if dx and dy then
		return dx,dy
	end
end
function moba.tilecollision(x1,y1,w1,h1,xm,ym,x2,y2,w2,h2)
	local x,y = moba.boxcollision(x1*32,y1*32,w1,h1,xm*32,ym*32,x2*32,y2*32,w2,h2)
	return x,y
end

addhook("always","moba.updatepreviouspos")
function moba.updatepreviouspos()
	for _,id in pairs(player(0,"table")) do
		if not moba.previouspos[id] then moba.previouspos[id] = {} end
		moba.previouspos[id].x = player(id,"x")
		moba.previouspos[id].y = player(id,"y")
	end
end

addhook("move","moba.playermove")
function moba.playermove(id,x,y)
	local ppos = moba.previouspos[id]
	local dx,dy
	for k,building in pairs(moba.buildings) do
		local sz = moba.config.buildings[building.type].size
		local tx,ty = moba.boxcollision(ppos.x-12,ppos.y-12,24,24,x-12,y-12,building.x-sz.w/2,building.y-sz.h/2,sz.w,sz.h)
		if tx and ty then
			if dx~=ppos.x then dx = tx+12 end
			if dy~=ppos.y then dy = ty+12 end
		end
	end
	for k,creep in pairs(moba.creeps) do
		local sz = moba.config.creeps[creep.type].size
		local tx,ty = moba.boxcollision(ppos.x-12,ppos.y-12,24,24,x-12,y-12,creep.x-sz.w/2,creep.y-sz.h/2,sz.w,sz.h)
		if tx and ty then
			if dx~=ppos.x then dx = tx+12 end
			if dy~=ppos.y then dy = ty+12 end
		end
	end
	if dx and dy then
		parse("setpos "..id.." "..dx.." "..dy)
	end
end